home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gas_251.zip / bin_251 / binutils / size.c < prev    next >
C/C++ Source or Header  |  1994-06-21  |  9KB  |  428 lines

  1. /* size.c -- report size of various sections of an executable file.
  2.    Copyright 1991, 92, 93, 94 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Binutils.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* Extensions/incompatibilities:
  21.    o - BSD output has filenames at the end.
  22.    o - BSD output can appear in different radicies.
  23.    o - SysV output has less redundant whitespace.  Filename comes at end.
  24.    o - SysV output doesn't show VMA which is always the same as the PMA.
  25.    o - We also handle core files.
  26.    o - We also handle archives.
  27.    If you write shell scripts which manipulate this info then you may be
  28.    out of luck; there's no --compatibility or --pedantic option.
  29. */
  30.  
  31. #include "bfd.h"
  32. #include "sysdep.h"
  33. #include "getopt.h"
  34. #include "bucomm.h"
  35.  
  36. #ifndef BSD_DEFAULT
  37. #define BSD_DEFAULT 1
  38. #endif
  39.  
  40. /* Program options.  */
  41.  
  42. enum
  43.   {
  44.     decimal, octal, hex
  45.   } radix = decimal;
  46. int berkeley_format = BSD_DEFAULT;    /* 0 means use AT&T-style output.  */
  47. int show_version = 0;
  48. int show_help = 0;
  49.  
  50. /* Program exit status.  */
  51. int return_code = 0;
  52.  
  53. /* IMPORTS */
  54. extern char *program_version;
  55. extern char *target;
  56.  
  57. /* Forward declarations */
  58.  
  59. static void display_file PARAMS ((char *filename));
  60.  
  61. static void print_sizes PARAMS ((bfd * file));
  62.  
  63. static void berkeley_sum PARAMS ((bfd *, sec_ptr, PTR));
  64.  
  65. void
  66. usage (stream, status)
  67.      FILE *stream;
  68.      int status;
  69. {
  70.   fprintf (stream, "\
  71. Usage: %s [-ABdoxV] [--format=berkeley|sysv] [--radix=8|10|16]\n\
  72.        [--target=bfdname] [--version] [--help] [file...]\n", program_name);
  73. #if BSD_DEFAULT
  74.   fputs ("default is --format=berkeley\n", stream);
  75. #else
  76.   fputs ("default is --format=sysv\n", stream);
  77. #endif
  78.   exit (status);
  79. }
  80.  
  81. struct option long_options[] =
  82. {
  83.   {"format", required_argument, 0, 200},
  84.   {"radix", required_argument, 0, 201},
  85.   {"target", required_argument, 0, 202},
  86.   {"version", no_argument, &show_version, 1},
  87.   {"help", no_argument, &show_help, 1},
  88.   {0, no_argument, 0, 0}
  89. };
  90.  
  91. int
  92. main (argc, argv)
  93.      int argc;
  94.      char **argv;
  95. {
  96.   int temp;
  97.   int c;
  98.  
  99.   program_name = *argv;
  100.   xmalloc_set_program_name (program_name);
  101.  
  102.   bfd_init ();
  103.  
  104.   while ((c = getopt_long (argc, argv, "ABVdox", long_options,
  105.                (int *) 0)) != EOF)
  106.     switch (c)
  107.       {
  108.       case 200:        /* --format */
  109.     switch (*optarg)
  110.       {
  111.       case 'B':
  112.       case 'b':
  113.         berkeley_format = 1;
  114.         break;
  115.       case 'S':
  116.       case 's':
  117.         berkeley_format = 0;
  118.         break;
  119.       default:
  120.         fprintf (stderr, "invalid argument to --format: %s\n", optarg);
  121.         usage (stderr, 1);
  122.       }
  123.     break;
  124.  
  125.       case 202:        /* --target */
  126.     target = optarg;
  127.     break;
  128.  
  129.       case 201:        /* --radix */
  130. #ifdef ANSI_LIBRARIES
  131.     temp = strtol (optarg, NULL, 10);
  132. #else
  133.     temp = atol (optarg);
  134. #endif
  135.     switch (temp)
  136.       {
  137.       case 10:
  138.         radix = decimal;
  139.         break;
  140.       case 8:
  141.         radix = octal;
  142.         break;
  143.       case 16:
  144.         radix = hex;
  145.         break;
  146.       default:
  147.         printf ("Invalid radix: %s\n", optarg);
  148.         usage (stderr, 1);
  149.       }
  150.     break;
  151.  
  152.       case 'A':
  153.     berkeley_format = 0;
  154.     break;
  155.       case 'B':
  156.     berkeley_format = 1;
  157.     break;
  158.       case 'V':
  159.     show_version = 1;
  160.     break;
  161.       case 'd':
  162.     radix = decimal;
  163.     break;
  164.       case 'x':
  165.     radix = hex;
  166.     break;
  167.       case 'o':
  168.     radix = octal;
  169.     break;
  170.       case 0:
  171.     break;
  172.       case '?':
  173.     usage (stderr, 1);
  174.       }
  175.  
  176.   if (show_version)
  177.     {
  178.       printf ("GNU %s version %s\n", program_name, program_version);
  179.       exit (0);
  180.     }
  181.   if (show_help)
  182.     usage (stdout, 0);
  183.  
  184.   if (optind == argc)
  185.     display_file ("a.out");
  186.   else
  187.     for (; optind < argc;)
  188.       display_file (argv[optind++]);
  189.  
  190.   return return_code;
  191. }
  192.  
  193. /* Display stats on file or archive member ABFD.  */
  194.  
  195. void
  196. display_bfd (abfd)
  197.      bfd *abfd;
  198. {
  199.   char **matching;
  200.  
  201.   if (bfd_check_format (abfd, bfd_archive))
  202.     /* An archive within an archive.  */
  203.     return;
  204.  
  205.   if (bfd_check_format_matches (abfd, bfd_object, &matching))
  206.     {
  207.       print_sizes (abfd);
  208.       printf ("\n");
  209.       return;
  210.     }
  211.  
  212.   if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
  213.     {
  214.       bfd_nonfatal (bfd_get_filename (abfd));
  215.       list_matching_formats (matching);
  216.       free (matching);
  217.       return_code = 3;
  218.       return;
  219.     }
  220.  
  221.   if (bfd_check_format_matches (abfd, bfd_core, &matching))
  222.     {
  223.       CONST char *core_cmd;
  224.  
  225.       print_sizes (abfd);
  226.       fputs (" (core file", stdout);
  227.  
  228.       core_cmd = bfd_core_file_failing_command (abfd);
  229.       if (core_cmd)
  230.     printf (" invoked as %s", core_cmd);
  231.  
  232.       puts (")\n");
  233.       return;
  234.     }
  235.  
  236.   bfd_nonfatal (bfd_get_filename (abfd));
  237.  
  238.   if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
  239.     {
  240.       list_matching_formats (matching);
  241.       free (matching);
  242.     }
  243.  
  244.   return_code = 3;
  245. }
  246.  
  247. static void
  248. display_archive (file)
  249.      bfd *file;
  250. {
  251.   bfd *arfile = (bfd *) NULL;
  252.  
  253.   for (;;)
  254.     {
  255.       bfd_set_error (bfd_error_no_error);
  256.  
  257.       arfile = bfd_openr_next_archived_file (file, arfile);
  258.       if (arfile == NULL)
  259.     {
  260.       if (bfd_get_error () != bfd_error_no_more_archived_files)
  261.         {
  262.           bfd_nonfatal (bfd_get_filename (file));
  263.           return_code = 2;
  264.         }
  265.       break;
  266.     }
  267.  
  268.       display_bfd (arfile);
  269.       /* Don't close the archive elements; we need them for next_archive */
  270.     }
  271. }
  272.  
  273. static void
  274. display_file (filename)
  275.      char *filename;
  276. {
  277.   bfd *file = bfd_openr (filename, target);
  278.   if (file == NULL)
  279.     {
  280.       bfd_nonfatal (filename);
  281.       return_code = 1;
  282.       return;
  283.     }
  284.  
  285.   if (bfd_check_format (file, bfd_archive) == true)
  286.     display_archive (file);
  287.   else
  288.     display_bfd (file);
  289.  
  290.   if (bfd_close (file) == false)
  291.     {
  292.       bfd_nonfatal (filename);
  293.       return_code = 1;
  294.       return;
  295.     }
  296. }
  297.  
  298. /* This is what lexical functions are for.  */
  299.  
  300. void
  301. lprint_number (width, num)
  302.      int width;
  303.      bfd_size_type num;
  304. {
  305.   printf ((radix == decimal ? "%-*lu\t" :
  306.        ((radix == octal) ? "%-*lo\t" : "%-*lx\t")),
  307.       width, (unsigned long) num);
  308. }
  309.  
  310. void
  311. rprint_number (width, num)
  312.      int width;
  313.      bfd_size_type num;
  314. {
  315.   printf ((radix == decimal ? "%*lu\t" :
  316.        ((radix == octal) ? "%*lo\t" : "%*lx\t")),
  317.       width, (unsigned long) num);
  318. }
  319.  
  320. static bfd_size_type bsssize;
  321. static bfd_size_type datasize;
  322. static bfd_size_type textsize;
  323.  
  324. static void
  325. berkeley_sum (abfd, sec, ignore)
  326.      bfd *abfd;
  327.      sec_ptr sec;
  328.      PTR ignore;
  329. {
  330.   bfd_size_type size;
  331.  
  332.   size = bfd_get_section_size_before_reloc (sec);
  333.   if (bfd_get_section_flags (abfd, sec) & SEC_CODE)
  334.     textsize += size;
  335.   else if (bfd_get_section_flags (abfd, sec) & SEC_DATA)
  336.     datasize += size;
  337.   else if (bfd_get_section_flags (abfd, sec) & SEC_ALLOC)
  338.     bsssize += size;
  339. }
  340.  
  341. void 
  342. print_berkeley_format (abfd)
  343.      bfd *abfd;
  344. {
  345.   static int files_seen = 0;
  346.   bfd_size_type total;
  347.  
  348.   bsssize = 0;
  349.   datasize = 0;
  350.   textsize = 0;
  351.  
  352.   bfd_map_over_sections (abfd, berkeley_sum, (PTR) NULL);
  353.  
  354.   if (files_seen++ == 0)
  355. #if 0
  356.     /* Intel doesn't like bss/stk because they don't have core files.  */
  357.     puts ((radix == octal) ? "text\tdata\tbss/stk\toct\thex\tfilename" :
  358.       "text\tdata\tbss/stk\tdec\thex\tfilename");
  359. #else
  360.     puts ((radix == octal) ? "text\tdata\tbss\toct\thex\tfilename" :
  361.       "text\tdata\tbss\tdec\thex\tfilename");
  362. #endif
  363.  
  364.   total = textsize + datasize + bsssize;
  365.  
  366.   lprint_number (7, textsize);
  367.   lprint_number (7, datasize);
  368.   lprint_number (7, bsssize);
  369.   printf (((radix == octal) ? "%-7lo\t%-7lx\t" : "%-7lu\t%-7lx\t"),
  370.       (unsigned long) total, (unsigned long) total);
  371.  
  372.   fputs (bfd_get_filename (abfd), stdout);
  373.   if (bfd_my_archive (abfd))
  374.     printf (" (ex %s)", bfd_get_filename (bfd_my_archive (abfd)));
  375. }
  376.  
  377. /* I REALLY miss lexical functions! */
  378. bfd_size_type svi_total = 0;
  379.  
  380. void
  381. sysv_internal_printer (file, sec, ignore)
  382.      bfd *file;
  383.      sec_ptr sec;
  384.      PTR ignore;
  385. {
  386.   bfd_size_type size = bfd_section_size (file, sec);
  387.   if (!bfd_is_abs_section (sec)
  388.       && !bfd_is_com_section (sec)
  389.       && !bfd_is_und_section (sec))
  390.     {
  391.       svi_total += size;
  392.  
  393.       printf ("%-12s", bfd_section_name (file, sec));
  394.       rprint_number (8, size);
  395.       printf (" ");
  396.       rprint_number (8, bfd_section_vma (file, sec));
  397.       printf ("\n");
  398.     }
  399. }
  400.  
  401. void
  402. print_sysv_format (file)
  403.      bfd *file;
  404. {
  405.   svi_total = 0;
  406.  
  407.   printf ("%s  ", bfd_get_filename (file));
  408.   if (bfd_my_archive (file))
  409.     printf (" (ex %s)", bfd_get_filename (bfd_my_archive (file)));
  410.  
  411.   puts (":\nsection\t\tsize\t     addr");
  412.   bfd_map_over_sections (file, sysv_internal_printer, (PTR) NULL);
  413.  
  414.   printf ("Total       ");
  415.   rprint_number (8, svi_total);
  416.   printf ("\n\n");
  417. }
  418.  
  419. static void
  420. print_sizes (file)
  421.      bfd *file;
  422. {
  423.   if (berkeley_format)
  424.     print_berkeley_format (file);
  425.   else
  426.     print_sysv_format (file);
  427. }
  428.